/**
 * 通过WebSocket推送实时信息警告记录
 */

package cn.web.tunnelrobot.websocket;


import cn.web.tunnelrobot.controller.WebSocketServer1;
import cn.web.tunnelrobot.dto.Warn;
import cn.web.tunnelrobot.dto.Warning;
import cn.web.tunnelrobot.util.Constants;
import com.alibaba.fastjson.JSONArray;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import lombok.SneakyThrows;
import java.io.IOException;
import java.util.*;
@Slf4j
@Component  // 被Spring容器管理
@Order(1)   // 如果多个自定义ApplicationRunner，用来表明执行顺序
public class PushAlarmWarning {
    public static void myTimer() {
        Runnable runnable1 = new Runnable() {
            @SneakyThrows
            @Override
            public void run() {
                while (true) {
                    Warn warn = new Warn();
                    List<Warning> list = Constants.warningList;
                    if (list.size() == 0){
                        warn.setCode(0);
                    }else {
                        warn.setCode(-1);
                        warn.setData(list);
                    }
                    try {
                        WebSocketServer1.sendInfo(JSONArray.toJSONString(warn)); // 推送
                    } catch (IOException e) {
//                        e.printStackTrace();
                    }
                    try {
                        Thread.sleep(1980);
                    } catch (InterruptedException e) {
//                        e.printStackTrace();
                    }
                }
            }
        };
        Thread thread1 = new Thread(runnable1);
        thread1.start();

    }
}
